package com.xxworkshop.dtcqcalculator;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.List;
import java.util.Map;

/**
 * Created by brochexu on 10/19/14.
 */
public class AddHeroActivity extends Activity {
    private DataManager dataManager = DataManager.getInstance();

    private ListView heroview;
    private ListView rankview;
    private LinearLayout equipmentview;

    private TextView hnameView;
    private TextView rnameView;

    private List<Map<String, Object>> heros;
    private List<Map<String, Object>> ranks;
    private List<Map<String, Object>> equipments;

    private long selectedHeroId = 0;
    private long selectedRankId = 0;

    private int index = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addhero);

        setResult(0);
        index = getIntent().getIntExtra("index", -1);
        if (index >= 0) {
            selectedHeroId = Global.getInstance().heros.get(index).heroid;
            selectedRankId = Global.getInstance().heros.get(index).rankid;
        }


        heros = dataManager.getHeros();
        ranks = dataManager.getRanks();

        heroview = (ListView) findViewById(R.id.addhero_herolist);
        rankview = (ListView) findViewById(R.id.addhero_ranklist);
        equipmentview = (LinearLayout) findViewById(R.id.addhero_equipmentlist);
        hnameView = (TextView) findViewById(R.id.hname);
        rnameView = (TextView) findViewById(R.id.rname);

        heroview.setAdapter(new MyAdapter(heros));
        rankview.setAdapter(new MyAdapter(ranks));

        heroview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectedHeroId = id;
                refreshEquipments();
            }
        });
        rankview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectedRankId = id;
                refreshEquipments();
            }
        });

        Button addButton = (Button) findViewById(R.id.addhero_add);
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectedHeroId == 0 || selectedRankId == 0) {
                    return;
                }

                HeroItem hi = new HeroItem();
                hi.heroid = selectedHeroId;
                hi.heroname = hnameView.getText().toString();
                hi.rankid = selectedRankId;
                hi.rankname = rnameView.getText().toString();

                int count = equipmentview.getChildCount();
                for (int i = 0; i < count; i++) {
                    CheckBox tview = (CheckBox) (equipmentview.getChildAt(i).findViewById(R.id.cname));
                    if (tview.isChecked()) {
                        hi.equipments.add((Long) tview.getTag());
                    }
                }
                if (index >= 0) {
                    Global.getInstance().heros.set(index, hi);
                } else {
                    Global.getInstance().heros.add(hi);
                }
                setResult(1);
                finish();
            }
        });
        Button deleteButton = (Button) findViewById(R.id.addhero_delete);
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (index >= 0) {
                    Global.getInstance().heros.remove(index);
                }
                setResult(1);
                finish();
            }
        });

        if (index >= 0) {
            new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    refreshEquipments();
                }
            }.sendEmptyMessageDelayed(0, 500);
        }
    }

    private void refreshEquipments() {
        hnameView.setText(dataManager.getHeroName(selectedHeroId));
        rnameView.setText(dataManager.getRankName(selectedRankId));
        equipmentview.removeAllViews();
        List<Map<String, Object>> edata = dataManager.getEquipments(selectedHeroId, selectedRankId);
        for (Map<String, Object> tdata : edata) {
            LinearLayout view = (LinearLayout) getLayoutInflater().inflate(R.layout.checkitem, equipmentview, false);
            CheckBox nameview = (CheckBox) view.findViewById(R.id.cname);
            nameview.setText(tdata.get("name").toString());
            nameview.setTag(Long.parseLong(tdata.get("id").toString()));
            if (index >= 0) {
                for (Long tid : Global.getInstance().heros.get(index).equipments) {
                    if (tid == Long.parseLong(tdata.get("id").toString())) {
                        nameview.setChecked(true);
                    }
                }
            }
            equipmentview.addView(view);
        }
    }

    private class MyAdapter extends BaseAdapter {
        private List<Map<String, Object>> dataSource;

        public MyAdapter(List<Map<String, Object>> dataSource) {
            this.dataSource = dataSource;
        }

        @Override
        public int getCount() {
            return dataSource.size();
        }

        @Override
        public Object getItem(int position) {
            return dataSource.get(position);
        }

        @Override
        public long getItemId(int position) {
            return Integer.parseInt(dataSource.get(position).get("id").toString());
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout item = (LinearLayout) getLayoutInflater().inflate(R.layout.listitem, parent, false);
            TextView tv = (TextView) item.findViewById(R.id.name);
            Map<String, Object> data = (Map<String, Object>) getItem(position);
            tv.setText(data.get("name").toString());
            item.setTag(getItemId(position));
            return item;
        }
    }
}
